đź”’ Password-Protected LED Vault
We used a tool called ChatGPT to help us come up with this project. Like all AI tools, it doesn't always get it right so thanks for being our testers!!
Welcome to the Password-Protected LED Vault project! You’ll build a system using a Raspberry Pi Pico, LEDs, and buttons. Let’s dive in and code it line by line!
Step 1: Import Required Libraries
In Python, we can use special libraries to make controlling the Pico easier. Let’s add the first line of code:
What does this do?
from machine
: This tells Python to use the machine library, which controls the Pico’s pins.Pin
: This lets us work with the Pico’s GPIO pins to control devices like LEDs and read buttons.
Step 2: Add a Delay Function
Sometimes we need to pause the program for a short time. Let’s add this line:
What does this do?
from time
: This imports the time library.sleep
: This function allows us to pause the program for a specific number of seconds.
🌟 Great work! You’ve set up the libraries. Let’s move on to controlling the LED and buttons.
Step 3: Set Up the LED
Now we’ll set up the LED so the Pico can control it. Add this line:
What does this do?
Pin(15, Pin.OUT)
: Configures GPIO15 as an output pin to control the LED.led
: A variable we’ll use to control the LED in our code.
Step 4: Set Up Buttons
Let’s add two buttons. Each will send a signal to the Pico when pressed:
What does this do?
Pin(14, Pin.IN)
: Configures GPIO14 as an input pin to read button presses.Pin.PULL_DOWN
: Ensures the button reads a “0” (off) when not pressed.button1
: A variable for the first button.
Now add the second button:
What does this do?
- It sets up a second button on GPIO13.
- We’ll use this button to represent “0” in the password.
🎉 Fantastic! You’ve connected the LED and buttons. Let’s start coding the password logic!
Step 5: Create a Password Function
Let’s create a function to handle passwords. Add this code:
What does this do?
def check_password()
: Creates a function namedcheck_password
.entered_password
: A list to store the user’s input.correct_password
: The password the user must match.
Step 6: Add Button Logic
Inside the function, add this loop to record button presses:
What does this do?
while
: Repeats until the full password is entered.button1.value()
: Checks if Button 1 is pressed.entered_password.append(1)
: Adds “1” to the password.sleep(0.3)
: Prevents accidental double presses.
Step 7: Check the Password
Finally, add this code to check if the password is correct:
What does this do?
if
: Compares the entered password to the correct one.led.value(1)
: Lights the LED if the password is correct.entered_password.clear()
: Resets the password for a new attempt.
🚀 You’ve built the full program! Save it as main.py
on your Pico and test it by entering the password (1, 0, 1).